﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UYD_WCS_AS_AGV_Controller.BaseClass
{
    public class CTaskStatusValue
    {
        //定义的委托 
        public delegate void dealData(object sender, EventArgs e);

        /// <summary>
        /// AGV任务状态变更事件
        /// </summary>
        public event dealData AgvReportTaskStatusChangeEvent;
        public void AgvReportTaskStatusChange()
        {
            if(AgvReportTaskStatusChangeEvent != null)
            AgvReportTaskStatusChangeEvent.Invoke(this, null);
        }
        private string agv_Report_Task_Status;
        /// <summary>
        /// AGV反馈的任务状态, 实际返回的是 msg.data 的值
        /// </summary>
        public string Agv_Report_Task_Status
        {
            get { return this.agv_Report_Task_Status; }
            set
            {
                if (value != this.agv_Report_Task_Status)
                {
                    this.agv_Report_Task_Status = value;
                    AgvReportTaskStatusChange();
                }
            }
        }

        private double agvBattery;
        /// <summary>
        /// AGV反馈的任务状态, 实际返回的是 msg.data 的值
        /// </summary>
        public double AgvBattery
        {
            get { return this.agvBattery; }
            set
            {
                if (value != this.agvBattery)
                {
                    this.agvBattery = value;
                }
            }
        }

        /// <summary>
        /// AGV自动充电事件
        /// </summary>
        public event dealData AgvAutoChangeEvent;
        public void AgvAutoChange_M()
        {
            if (AgvAutoChangeEvent != null)
                AgvAutoChangeEvent.Invoke(this, null);
        }
        private int agvAutoChange;
        /// <summary>
        /// AGV自动充电逻辑
        /// </summary>
        public int AgvAutoChange
        {
            get { return this.agvAutoChange; }
            set
            {
                if (value != this.agvAutoChange)
                {
                    this.agvAutoChange = value;
                    if (this.agvAutoChange == 1)
                        AgvAutoChange_M();
                }
            }
        }
        /// <summary>
        /// 任务状态变更事件
        /// </summary>
        public event dealData TaskStatusChangeEvent;
        public void TaskStatusChange()
        {
            TaskStatusChangeEvent.Invoke(this, null);
        }
        private int task_Status=-1;
        /// <summary> 
        /// 实时任务状态 0：空闲 /1：待执行/2：取货行走中/3：到达取货位/4：取货完成/5：放货行走中/6：到达放货位/7：放货完成/8：放货返回/9：任务完成/10:任务取消
        /// </summary>
        public int Task_Status
        {
            get { return this.task_Status; }
            set
            {
                if (value != this.task_Status)
                {
                    this.task_Status = value;
                    //TaskStatusChange();
                }
            }
        }




        ///// <summary>
        ///// 接收到新任务事件，存储到数据库中
        ///// </summary>
        //public event dealData ReceiveNewTaskEvent;
        //public void ReceiveNewTask_M()
        //{
        //    ReceiveNewTaskEvent.Invoke(this, null);
        //}
        //private string receiveTask = "";
        ///// <summary>
        ///// 接收到新任务
        ///// </summary>
        //public string ReceiveNewTask
        //{
        //    get { return this.receiveTask; }
        //    set
        //    {
        //        //if (value != this.receiveTask)
        //        //{
        //        this.receiveTask = value;
        //        //ReceiveNewTask_M();
        //        //}
        //    }
        //}


        /// <summary>
        /// 接收到任务取消任务事件，存储到数据库中
        /// </summary>
        public event dealData ReceiveTaskCancleEvent;
        public void ReceiveTaskCancle_M()
        {
            ReceiveTaskCancleEvent.Invoke(this, null);
        }
        private string receiveTaskCancle = "";
        /// <summary>
        /// 接收到任务取消
        /// </summary>
        public string ReceiveTaskCancle
        {
            get { return this.receiveTaskCancle; }
            set
            {
                if (value != this.receiveTaskCancle)
                {
                    this.receiveTaskCancle = value;
                    ReceiveTaskCancle_M();
                }
            }
        }

        #region 堆垛机相关读取事件定义，此处用货叉的




        private int srm_Get_Finish;
        /// <summary>
        /// 堆垛机取货完成事件处理
        /// </summary>
        public int SRM_Get_Finish
        {
            get { return this.srm_Get_Finish; }
            set
            {
                if (value != this.srm_Get_Finish)
                {
                    this.srm_Get_Finish = value;
                    if (this.srm_Get_Finish == 1)
                        SRM_GetFinish();
                    //else if (this.srm_Get_Finish == 0)
                    //    SRM_GetFinishFlushPage();
                }
            }
        }

        /// <summary>
        /// 堆垛机取货完成监控事件
        /// </summary>
        public event dealData SRM_Get_Finish_Event;
        public void SRM_GetFinish()
        {
            SRM_Get_Finish_Event.Invoke(this, null);
        }

        /// <summary>
        /// 堆垛机取货完成后刷新页面事件
        /// </summary>
        public event dealData SRM_Get_Finish_Flush_Page_Event;
        public void SRM_GetFinishFlushPage()
        {
            SRM_Get_Finish_Flush_Page_Event.Invoke(this, null);
        }

        /// <summary>
        /// 堆垛机放货完成监控事件 出要用在出库
        /// </summary>
        public event dealData SRM_Set_Finish_Event;
        public void SRM_SetFinish()
        {
            SRM_Set_Finish_Event.Invoke(this, null);
        }
        private int srm_Set_Finish;
        /// <summary>
        /// 堆垛机放货完成事件处理，主要用在出库
        /// </summary>
        public int SRM_Set_Finish
        {
            get { return this.srm_Set_Finish; }
            set
            {
                if (value != this.srm_Set_Finish)
                {
                    this.srm_Set_Finish = value;
                    if (this.srm_Set_Finish == 1)
                        SRM_SetFinish();
                    //else if (this.srm_Set_Finish == 0)
                    //    SRM_SetFinishFlushPage();
                }
            }
        }

        /// <summary>
        /// 堆垛机放货完成后刷新页面事件
        /// </summary>
        public event dealData SRM_Set_Finish_Flush_Page_Event;
        public void SRM_SetFinishFlushPage()
        {
            SRM_Set_Finish_Flush_Page_Event.Invoke(this, null);
        }

        private int srm_Task_Finish;
        /// <summary>
        /// 堆垛机任务完成 ,此处表示货叉的移动任务完成
        /// </summary>
        public int SRM_Task_Finish
        {
            get { return this.srm_Task_Finish; }
            set
            {

                if (value != this.srm_Task_Finish)
                {
                    this.srm_Task_Finish = value;
                    if (this.srm_Task_Finish == 1)
                        SRM_TaskFinish();
                    //else if (this.srm_Task_Finish == 0)
                    //    SRM_TaskFinishFlushPage();
                }
            }
        }

        /// <summary>
        /// 堆垛机任务完成监控事件
        /// </summary>
        public event dealData SRM_Task_Finish_Event;
        public void SRM_TaskFinish()
        {
            SRM_Task_Finish_Event.Invoke(this, null);
        }

        /// <summary>
        /// 堆垛机任务完成后刷新页面事件
        /// </summary>
        public event dealData SRM_Task_Finish_Flush_Page_Event;
        public void SRM_TaskFinishFlushPage()
        {
            SRM_Task_Finish_Flush_Page_Event.Invoke(this, null);
        }

        private int srm_BreakDownInfo;
        /// <summary>
        /// 获取堆垛机错误信息
        /// </summary>
        public int SRM_BreakDownInfo
        {
            get { return this.srm_BreakDownInfo; }
            set
            {
                if (value != this.srm_BreakDownInfo)
                {
                    if (value == 14)
                    {
                        AGV_Estop();
                    }
                    else if (value == 0 && this.SRM_BreakDownInfo == 14)
                    {
                        AGV_Reset();
                    }
                    this.srm_BreakDownInfo = value;

                    SRMBreakDown();
                }
            }
        }
        /// <summary>
        /// 获取到堆垛机错误后的处理事件
        /// </summary>
        public event dealData SRMBreakDownEvent;
        public void SRMBreakDown()
        {
            if (SRMBreakDownEvent!=null)
            {
                SRMBreakDownEvent.Invoke(this, null);
            }
        }

        public event dealData AGV_Estop_Event;
        public void AGV_Estop()
        {
            AGV_Estop_Event.Invoke(this, null);
        }
        public event dealData AGV_Reset_Event;
        public void AGV_Reset()
        {
            AGV_Reset_Event.Invoke(this, null);
        }
        private int srm_Status = -1;
        /// <summary>
        /// 堆垛机当前状态监控
        /// </summary>
        public int SRM_Status
        {
            get { return this.srm_Status; }
            set
            {
                if (value != this.srm_Status)
                {
                    this.srm_Status = value;
                    //SRM_StatusInfo();
                }
            }
        }
        /// <summary>
        /// 堆垛机当前状态监控变化事件
        /// </summary>
        public event dealData SRM_StatusEvent;
        public void SRM_StatusInfo()
        {
            try
            {
                SRM_StatusEvent.Invoke(this, null);
            }
            catch
            {

            }
        }




        private int srm_RemoteAuto = -1;
        /// <summary>
        /// 堆垛机当前是否是远程自动模式
        /// </summary>
        public int SRM_RemoteAuto
        {
            get { return this.srm_RemoteAuto; }
            set
            {
                if (value != this.srm_RemoteAuto)
                {
                    this.srm_RemoteAuto = value;
                    //SRM_RemoteAutoInfo();
                }
            }
        }
        /// <summary>
        /// 堆垛机当前状态监控变化事件
        /// </summary>
        public event dealData SRM_RemoteAutoEvent;
        public void SRM_RemoteAutoInfo()
        {
            try
            {
                SRM_RemoteAutoEvent.Invoke(this, null);
            }
            catch
            {

            }
        }


        private int srm_Heartbeat;
        /// <summary>
        /// 堆垛机心跳事件处理
        /// </summary>
        public int SRM_Heartbeat
        {
            get { return this.srm_Heartbeat; }
            set
            {
                if (value != this.srm_Heartbeat)
                {
                    this.srm_Heartbeat = value;
                    //SRM_HeartbeatFlush();
                }
            }
        }
        /// <summary>
        /// 堆垛机心跳处理事件
        /// </summary>
        public event dealData SRM_Heartbeat_Event;
        public void SRM_HeartbeatFlush()
        {
            SRM_Heartbeat_Event.Invoke(this, null);
        }


        private int srm_Alarm = -1;
        /// <summary>
        /// 堆垛机是否有报警
        /// </summary>
        public int SRM_Alarm
        {
            get { return this.srm_Alarm; }
            set
            {
                if (value != this.srm_Alarm)
                {
                    this.srm_Alarm = value;
                   // SRM_AlarmInfo();
                }
            }
        }
        /// <summary>
        /// 堆垛机是否有报警
        /// </summary>
        public event dealData SRM_AlarmEvent;
        public void SRM_AlarmInfo()
        {
            if (SRM_AlarmEvent!=null)
            {
                SRM_AlarmEvent.Invoke(this, null);
            }
        }


        private int srm_ForkInZero = -1;
        /// <summary>
        /// 堆垛机货叉是否在0点
        /// </summary>
        public int SRM_ForkInZero
        {
            get { return this.srm_ForkInZero; }
            set
            {
                if (value != this.srm_ForkInZero)
                {
                    this.srm_ForkInZero = value;
                    //SRM_ForkInZero_M();
                }
            }
        }
        /// <summary>
        /// 堆垛机是否有报警
        /// </summary>
        public event dealData SRM_ForkInZeroEvent;
        public void SRM_ForkInZero_M()
        {
            SRM_ForkInZeroEvent.Invoke(this, null);
        }

        private int srm_ForkIsHasGood = -1;
        /// <summary>
        /// 堆垛机货叉是否有货
        /// </summary>
        public int SRM_ForkIsHasGood
        {
            get { return this.srm_ForkIsHasGood; }
            set
            {
                if (value != this.srm_ForkIsHasGood)
                {
                    this.srm_ForkIsHasGood = value;
                    //SRM_ForkIsHasGood_M();
                }
            }
        }
        /// <summary>
        /// 堆垛机是否有货
        /// </summary>
        public event dealData SRM_ForkIsHasGoodEvent;
        public void SRM_ForkIsHasGood_M()
        {
            SRM_ForkIsHasGoodEvent.Invoke(this, null);
        }



        private int srm_Pos_Row = -1;
        /// <summary>
        /// 堆垛机当前所在位置_排
        /// </summary>
        public int SRM_Pos_Row
        {
            get { return this.srm_Pos_Row; }
            set
            {
                if (value != this.srm_Pos_Row)
                {
                    this.srm_Pos_Row = value;
                    //SRM_Pos_Row_M();
                }
            }
        }
        /// <summary>
        /// 堆垛机当前所在位置_排
        /// </summary>
        public event dealData SRM_Pos_RowEvent;
        public void SRM_Pos_Row_M()
        {
            SRM_Pos_RowEvent.Invoke(this, null);
        }


        private int srm_Pos_Floor = -1;
        /// <summary>
        /// 堆垛机当前所在位置_层
        /// </summary>
        public int SRM_Pos_Floor
        {
            get { return this.srm_Pos_Floor; }
            set
            {
                if (value != this.srm_Pos_Floor)
                {
                    this.srm_Pos_Floor = value;
                    //SRM_Pos_Floor_M();
                }
            }
        }
        /// <summary>
        /// 堆垛机当前所在位置_层
        /// </summary>
        public event dealData SRM_Pos_FloorEvent;
        public void SRM_Pos_Floor_M()
        {
            SRM_Pos_FloorEvent.Invoke(this, null);
        }


        private int srm_Task_ID = -1;
        /// <summary>
        /// 堆垛机当前任务ID
        /// </summary>
        public int SRM_Task_ID
        {
            get { return this.srm_Task_ID; }
            set
            {
                if (value != this.srm_Task_ID)
                {
                    this.srm_Task_ID = value;
                    //SRM_Task_ID_M();
                }
            }
        }
        /// <summary>
        /// 堆垛机当前任务ID
        /// </summary>
        public event dealData SRM_Task_IDEvent;
        public void SRM_Task_ID_M()
        {
            SRM_Task_IDEvent.Invoke(this, null);
        }




        public event dealData SRM_GetGoodsApplyEnent;
        public void SRMGetGoodsApply()
        {
            SRM_GetGoodsApplyEnent.Invoke(this, null);
        }
        private int srm_GetGoodsApply;
        /// <summary>
        /// 堆垛机取货申请
        /// </summary>
        public int SRM_GetGoodsApply
        {
            get { return this.srm_GetGoodsApply; }
            set
            {
                if (value != this.srm_GetGoodsApply)
                {
                    this.srm_GetGoodsApply = value;
                    if(value==1)
                        SRMGetGoodsApply();
                }
            }
        }


        public event dealData SRM_SetGoodsApplyEnent;
        public void SRMSetGoodsApply()
        {
            SRM_SetGoodsApplyEnent.Invoke(this, null);
        }
        private int srm_setGoodsApply;
        /// <summary>
        /// 堆垛机放货申请
        /// </summary>
        public int SRM_SetGoodsApply
        {
            get { return this.srm_setGoodsApply; }
            set
            {
                if (value != this.srm_setGoodsApply)
                {
                    this.srm_setGoodsApply = value;
                    if (value == 1)
                        SRMSetGoodsApply();
                }
            }
        }



        #endregion


        #region 1号堆垛机定义的变量和地址

        #region WcsWirteToPlc

        /// <summary>
        /// 堆垛机心跳对应的PLC地址 DB540.0.0
        /// </summary>
        public string SRM_Heartbeat_ToPlc_Addr = "DB540.0.0";
        /// <summary>
        /// 堆垛机复位对应的PLC地址 DB540.2.0
        /// </summary>
        public string SRM_ReSet_ToPlc_Addr = "DB540.2.0";
        /// <summary>
        /// 堆垛机远程自动PLC地址 DB540.4.0
        /// </summary>
        public string SRM_RemoteAuto_ToPlc_Addr = "DB540.4.0";
        /// <summary>
        /// 堆垛机放排 对应的PLC地址 DB540.6.0
        /// </summary>
        public string SRM_SetRow_ToPlc_Addr = "DB540.6.0";

        /// <summary> 
        /// 堆垛机放列 对应的PLC地址 DB540.8.0
        /// </summary>
        public string SRM_SetColl_ToPlc_Addr = "DB540.8.0";

        /// <summary>
        /// 堆垛机放层 对应的PLC地址 DB540.10.0
        /// </summary>
        public string SRM_SetFloor_ToPlc_Addr = "DB540.10.0";
        /// <summary>
        /// 堆垛机任务继续 DB540.12.0
        /// </summary>
        public string SRM_Task_Continue_ToPlc_Addr = "DB540.12.0";

        /// <summary>
        /// 堆垛机任务清除 对应的PLC地址 DB540.14.0
        /// </summary>
        public string SRM_Task_Clear_ToPlc_Addr = "DB540.14.0";

        /// <summary>
        /// 堆垛机急停对应的PLC地址 DB540.16.0
        /// </summary>
        public string SRM_EmergencyStop_ToPlc_Addr = "DB540.16.0";

        /// <summary>
        /// 堆垛机任务ID 对应的PLC地址 DB540.18.0
        /// </summary>
        public string SRM_TaskID_ToPlc_Addr = "DB540.18.0";

        /// <summary>
        /// 堆垛机启动标志 对应的PLC地址 DB540.20.0
        /// </summary>
        public string SRM_TaskStartFlag_ToPlc_Addr = "DB540.20.0";


        /// <summary>
        /// 堆垛机任务类型 =1移库，=2取货，=3放货 对应的PLC地址 DB540.22.0
        /// </summary>
        public string SRM_TaskType_ToPlc_Addr = "DB540.22.0";

        /// <summary>
        /// 堆垛机取货允许
        /// </summary>
        public string SRM_GetGoodsAllow_ToPlc_Addr = "DB540.44.0";
        /// <summary>
        /// 堆垛机放货允许
        /// </summary>
        public string SRM_SetGoodsAllow_ToPlc_Addr = "DB540.46.0";
        #endregion
        #region WcsReadFromPLC

        /// <summary>
        /// 堆垛机心跳发送给WCS对应的PLC地址 DB540.24.0
        /// </summary>
        public string SRM_Hearbeat_ToWcs_Addr = "DB540.24.0";
        /// <summary>
        /// 堆垛机取货完成对应的PLC地址 DB540.24.1
        /// </summary>
        public string SRM_Get_Finished_ToWcs_Addr = "DB540.24.1";
        /// <summary>
        /// 堆垛机放货完成对应的PLC地址 DB540.24.2
        /// </summary>
        public string SRM_Set_Finished_ToWcs_Addr = "DB540.24.2";
        /// <summary>
        /// 堆垛机远程自动模式对应的PLC地址 DB540.24.3
        /// </summary>
        public string SRM_RemoteAuto_ToWcs_Addr = "DB540.24.3";
        /// <summary>
        /// 堆垛机报警信号对应的PLC地址 DB540.24.4
        /// </summary>
        public string SRM_Alarm_ToWcs_Addr = "DB540.24.4";
        /// <summary>
        /// 堆垛机任务完成对应的PLC地址 DB540.24.5
        /// </summary>
        public string SRM_Task_Finished_ToWcs_Addr = "DB540.24.5";
        /// <summary>
        /// 堆垛机载货台是否有货对应的PLC地址 DB540.24.6
        /// </summary>
        public string SRM_ForkIsHasGoods_ToWcs_Addr = "DB540.24.6";
        /// <summary>
        /// 堆垛机货叉是否在原点对应的PLC地址 DB540.24.7
        /// </summary>
        public string SRM_ForkInZero_ToWcs_Addr = "DB540.24.7";


        /// <summary>
        /// 堆垛机报警代码对应的PLC地址 DB540.26.0
        /// </summary>
        public string SRM_AlarmCode_ToWcs_Addr = "DB540.26.0";
        /// <summary>
        /// 堆垛机状态对应的PLC地址 DB540.28.0
        /// </summary>
        public string SRM_Status_ToWcs_Addr = "DB540.28.0";
        /// <summary>
        /// 堆垛机当前位置所在排对应的PLC地址 DB540.30.0
        /// </summary>
        public string SRM_Pos_Row_ToWcs_Addr = "DB540.30.0";
        /// <summary>
        /// 堆垛机当前位置所在层对应的PLC地址 DB540.32.0
        /// </summary>
        public string SRM_Pos_Floor_ToWcs_Addr = "DB540.32.0";
        /// <summary>
        /// 堆垛机当前任务ID对应的PLC地址 DB540.42.0
        /// </summary>
        public string SRM_TaskID_ToWcs_Addr = "DB540.42.0";
        #endregion
        /// <summary>
        /// 货叉手自动控制PLC地址 DB540.52.0
        /// </summary>
        public string SRM_HandAuto_ToPlc_Addr = "DB540.52.0";
        /// <summary>
        /// 货叉上移PLC地址 DB540.54.0
        /// </summary>
        public string SRM_UpMove_ToPlc_Addr = "DB540.54.0";
        /// <summary>
        /// 货叉下移PLC地址 DB540.56.0
        /// </summary>
        public string SRM_DownMove_ToPlc_Addr = "DB540.56.0";
        /// <summary>
        /// 货叉左移PLC地址 DB540.58.0
        /// </summary>
        public string SRM_LeftMove_ToPlc_Addr = "DB540.58.0";
        /// <summary>
        /// 货叉右移PLC地址 DB540.60.0
        /// </summary>
        public string SRM_RightMove_ToPlc_Addr = "DB540.60.0";

        #endregion
    }
}
